package com.oceansoft.core.jxkh.dao.impl;

import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.dao.ITaskDao;
import com.oceansoft.core.jxkh.domain.Khfk;
import com.oceansoft.core.jxkh.domain.Task;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by begcoffee on 2017/12/11.
 */
@Repository
public class TaskDaoImpl implements ITaskDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Pagination querybycondition(HashMap<String, String> map, SysUser user) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select * from t_pskh_task t where 1=1 ";
        if (map.containsKey("checkyear") && StringUtil.isNotNullAndBlank(map.get("checkyear"))&& !"-1".equals(map.get("checkyear"))) {
            sql += " and checkyear ='" + map.get("checkyear") + "' ";
        }
        if (map.containsKey("checktype") && StringUtil.isNotNullAndBlank(map.get("checktype"))&& !"-1".equals(map.get("checktype"))) {
            sql += " and type ='" + map.get("checktype") + "' ";
        }
        if (map.containsKey("checkstatus") && StringUtil.isNotNullAndBlank(map.get("checkstatus"))&& !"-1".equals(map.get("checkstatus"))) {
            sql += " and status ='" + map.get("checkstatus") + "' ";
        }
        sql += " order by t.checkyear,t.type,t.checkno ";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset, limit);
        List list = jdbcTemplate.queryForList(page_sql);
//        System.out.println(sql);
        return new Pagination(list, _total, limit, (offset / limit) + 1);
    }


    @Override
    public void addTask(Task task) throws Exception {
        String sql = "insert into t_pskh_task (DBID,NAME,TYPE,CHECKYEAR,CHECKNO,BASEMARK,STARTDATE,ENDDATE,REPLAYTYPE,REPLAYWAY,REPLAYPOINT,STATUS) ";
        sql += " values (SEQ_PSKH_TASK.nextval,?,?,?,?,?,?,?,'0','0','0','0') ";
        jdbcTemplate.update(sql, task.getName(), task.getType(), task.getCheckyear(), task.getCheckno(), task.getBasemark(), task.getStartdate(), task.getEnddate());
    }

    @Override
    public void saveTask(Task task) throws Exception {
        String sql = "update t_pskh_task set name=?,type=?,checkyear=?,checkno=?,basemark=?,startdate=?,enddate=?,status=? where dbid =? ";
        jdbcTemplate.update(sql, task.getName(), task.getType(), task.getCheckyear(), task.getCheckno(), task.getBasemark(), task.getStartdate(), task.getEnddate(),task.getStatus(),task.getDbid());
    }
    @Override
    public void deleteTask(Long dbid) throws Exception {
        String sql = "delete from t_pskh_task where dbid = ?";
        jdbcTemplate.update(sql, dbid);
    }

    @Override
    public int stopTask(Long dbid,String type) throws Exception {
        String sql = "";
        if("0".equals(type)) {
            sql = "update t_pskh_task set status=4 where status=0 and dbid =? ";
        }else{
            sql = "update t_pskh_task set status=0 where status=4 and dbid =? ";
        }
        int result = jdbcTemplate.update(sql,dbid);
        return result;
    }

    @Override
    public Task getTaskById(Long dbid) throws Exception {
        String sql = "select * from t_pskh_task where dbid = "+dbid;

        Task task = jdbcTemplate.queryForObject(sql, new RowMapper<Task>() {
            @Override
            public Task mapRow(ResultSet rs, int arg1) throws SQLException {
                Task task1 = new Task();
                task1.setDbid(rs.getInt("DBID"));
                task1.setName(rs.getString("NAME"));
                task1.setType(rs.getString("TYPE"));
                task1.setCheckyear(rs.getString("CHECKYEAR"));
                task1.setCheckno(rs.getString("CHECKNO"));
                task1.setBasemark(rs.getLong("BASEMARK"));
                task1.setStatus(rs.getString("STATUS"));
                task1.setStartdate(rs.getDate("STARTDATE"));
                task1.setEnddate(rs.getDate("ENDDATE"));
                task1.setReplaypoint(rs.getString("REPLAYPOINT"));
                task1.setReplaytype(rs.getString("REPLAYTYPE"));
                task1.setReplayway(rs.getString("REPLAYWAY"));
                return task1;
            }
        });

        return task;
    }

    @Override
    public void saveKhfk(Khfk khfk) throws Exception {
        String sql = "update t_pskh_khfk set bmfkfs=?,bmfknr=?,bmfkjd=?,ryfkfs=?,ryfknr=?,ryfkjd=?,updateuser=? where dbid =?";
        jdbcTemplate.update(sql,khfk.getBmfkfs(),khfk.getBmfknr(),khfk.getBmfkjd(),khfk.getRyfkfs(),khfk.getRyfknr(),khfk.getRyfkjd(),khfk.getUpdateuser(),khfk.getDbid());
    }

    @Override
    public Khfk getKhfkById(Long dbid) throws Exception {
        String sql = "select * from t_pskh_khfk where dbid = "+dbid;

        Khfk khfk = jdbcTemplate.queryForObject(sql, new RowMapper<Khfk>() {
            @Override
            public Khfk mapRow(ResultSet rs, int arg1) throws SQLException {
                Khfk khfk1 = new Khfk();
                khfk1.setDbid(rs.getInt("DBID"));
                khfk1.setBmfkfs(rs.getString("BMFKFS"));
                khfk1.setBmfknr(rs.getString("BMFKNR"));
                khfk1.setBmfkjd(rs.getString("BMFKJD"));
                khfk1.setRyfkfs(rs.getString("RYFKFS"));
                khfk1.setRyfknr(rs.getString("RYFKNR"));
                khfk1.setRyfkjd(rs.getString("RYFKJD"));
                return khfk1;
            }
        });
        return khfk;
    }

    @Override
    public List<Task> getEndingTask() throws Exception {
        String sql = "select * from t_pskh_task where enddate < sysdate  and status = "+Constant.TASKDOING;

        List<Task> list = jdbcTemplate.query(sql, new RowMapper<Task>() {
            @Override
            public Task mapRow(ResultSet rs, int arg1) throws SQLException {
                Task task1 = new Task();
                task1.setDbid(rs.getInt("DBID"));
                task1.setName(rs.getString("NAME"));
                task1.setType(rs.getString("TYPE"));
                task1.setCheckyear(rs.getString("CHECKYEAR"));
                task1.setCheckno(rs.getString("CHECKNO"));
                task1.setBasemark(rs.getLong("BASEMARK"));
                task1.setStatus(rs.getString("STATUS"));
                task1.setStartdate(rs.getDate("STARTDATE"));
                task1.setEnddate(rs.getDate("ENDDATE"));
                task1.setReplaypoint(rs.getString("REPLAYPOINT"));
                task1.setReplaytype(rs.getString("REPLAYTYPE"));
                task1.setReplayway(rs.getString("REPLAYWAY"));
                return task1;
            }
        });
        return list;
    }

    @Override
    public List<Task> getStartingTask() throws Exception {
        String sql = "select * from t_pskh_task where startdate < sysdate  and status = "+Constant.TASKREADY;

        List<Task> list = jdbcTemplate.query(sql, new RowMapper<Task>() {
            @Override
            public Task mapRow(ResultSet rs, int arg1) throws SQLException {
                Task task1 = new Task();
                task1.setDbid(rs.getInt("DBID"));
                task1.setName(rs.getString("NAME"));
                task1.setType(rs.getString("TYPE"));
                task1.setCheckyear(rs.getString("CHECKYEAR"));
                task1.setCheckno(rs.getString("CHECKNO"));
                task1.setBasemark(rs.getLong("BASEMARK"));
                task1.setStatus(rs.getString("STATUS"));
                task1.setStartdate(rs.getDate("STARTDATE"));
                task1.setEnddate(rs.getDate("ENDDATE"));
                task1.setReplaypoint(rs.getString("REPLAYPOINT"));
                task1.setReplaytype(rs.getString("REPLAYTYPE"));
                task1.setReplayway(rs.getString("REPLAYWAY"));
                return task1;
            }
        });
        return list;
    }

    @Override
    public void endTask(Task task) throws Exception {

    }

    @Override
    public Task getNextTask(Task task) throws Exception {
        String sql = "select * from t_pskh_task where status = " + Constant.TASKWAIT;
        sql += " and TYPE = " + task.getType();
        int Checkno = Integer.valueOf(task.getCheckno());
        if(Checkno<12){
            sql += " and CHECKYEAR = " + task.getCheckyear();
            sql += " and CHECKNO = " + (Checkno+1);
        }else {
            sql += " and CHECKYEAR = " + (Integer.valueOf(task.getCheckyear())+1);
            sql += " and CHECKNO = 1 ";
        }

        List<Task> list = jdbcTemplate.query(sql, new RowMapper<Task>() {
            @Override
            public Task mapRow(ResultSet rs, int arg1) throws SQLException {
                Task task1 = new Task();
                task1.setDbid(rs.getInt("DBID"));
                task1.setName(rs.getString("NAME"));
                task1.setType(rs.getString("TYPE"));
                task1.setCheckyear(rs.getString("CHECKYEAR"));
                task1.setCheckno(rs.getString("CHECKNO"));
                task1.setBasemark(rs.getLong("BASEMARK"));
                task1.setStatus(rs.getString("STATUS"));
                task1.setStartdate(rs.getDate("STARTDATE"));
                task1.setEnddate(rs.getDate("ENDDATE"));
                task1.setReplaypoint(rs.getString("REPLAYPOINT"));
                task1.setReplaytype(rs.getString("REPLAYTYPE"));
                task1.setReplayway(rs.getString("REPLAYWAY"));
                return task1;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateTaskStatus(Task task) throws Exception {
        String sql = "update t_pskh_task set status=? where dbid =?";
        jdbcTemplate.update(sql,task.getStatus(),task.getDbid());
    }

    @Override
    public void doCopyTaskRelation(Task curentTask, Task nextTask) throws Exception {
        //考核人
        String sql = "insert into t_pskh_task_checker select seq_pskh_task_checker.nextval, ? ,a.deptno,a.checkerid,a.checkername from t_pskh_task_checker a where a.taskid= ? ";
        jdbcTemplate.update(sql,nextTask.getDbid(),curentTask.getDbid());

        //考核关系
        sql = "insert into t_pskh_task_relation select seq_pskh_task_relation.nextval,(select min(dbid) from t_pskh_task_checker c where c.checkerid=a.checkerid and c.taskid = ?) as checkerdbid,b.becheckerid,b.becheckername,b.weight from t_pskh_task_checker a,t_pskh_task_relation b where a.dbid=b.checkerdbid and a.taskid= ? ";
        jdbcTemplate.update(sql,nextTask.getDbid(),curentTask.getDbid());
    }

    @Override
    public void initTaskMark(Task task) throws Exception {
        String sql ="insert into t_pskh_mark_my " +
                        "  (dbid, taskid, becheckerid, status, updatetime) " +
                        "  select SEQ_PSKH_MARK_MY.NEXTVAL, a.taskid, b.becheckerid, '0', sysdate " +
                        "    from t_pskh_task_checker a, t_pskh_task_relation b " +
                        "   where a.dbid = b.checkerdbid " +
                        "     and a.taskid = ? ";
        jdbcTemplate.update(sql,task.getDbid());

        sql ="insert into t_pskh_mark_item(dbid,markid,relationid,type,status,updatetime) " +
                        "select SEQ_PSKH_MARK_ITEM.NEXTVAL, " +
                        "           a.dbid, " +
                        "           b.relationid, " +
                        "           b.type, " +
                        "           '0' as status, " +
                        "           sysdate as updatetime " +
                        "      from t_pskh_mark_my a, v_pskh_task_relation b " +
                        "     where a.taskid = b.taskid " +
                        "       and a.becheckerid = b.becheckerid " +
                        "       and a.taskid = ? ";
        jdbcTemplate.update(sql,task.getDbid());
    }
}
